package com.jinchengll.xigua.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.jinchengll.xigua.R;
import com.jinchengll.xigua.ui.base.BaseActivity;
import com.jinchengll.xigua.ui.fragment.BlankFragment;
import com.jinchengll.xigua.ui.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private final String LOG_TAG = "MainActivity";

    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        viewPager = findViewById(R.id.mainViewPage2);
        // 首页不需要滑动
        viewPager.setUserInputEnabled(false);
        tabLayout = findViewById(R.id.mainTabLayout);
    }

    @Override
    protected void initData() {
        String[] tabTexts = getResources().getStringArray(R.array.home_tab_text);
        // 初始化viewpage
        List<Fragment> fragments = new ArrayList<>();
        for (String text : tabTexts) {
            if ("首页".equals(text)) {
                fragments.add(HomeFragment.newInstance());
            } else {
                fragments.add(BlankFragment.newInstance(text));
            }
        }
        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });
        // 初始化TabLayout
        int[] tabSelectedImages = {
                R.drawable.home_selected,
                R.drawable.show_selected,
                R.drawable.add_selected,
                R.drawable.message_selected,
                R.drawable.me_selected
        };
        int[] tabUnselectedImages = {
                R.drawable.home_unselected,
                R.drawable.show_unselected,
                R.drawable.add_unselected,
                R.drawable.message_unselected,
                R.drawable.me_unselected
        };
        for (int i = 0; i < tabTexts.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setCustomView(R.layout.tab_item_main);
            View customView = tab.getCustomView();
            // todo 就两个控件，先不使用ViewHolder来提升性能
            ImageView itemImage = customView.findViewById(R.id.itemImage);
            itemImage.setImageResource(i == 0 ? tabSelectedImages[i] : tabUnselectedImages[i]);
            TextView itemText = customView.findViewById(R.id.itemText);
            itemText.setText(tabTexts[i]);
            int textColorId = i == 0 ? R.color.black : R.color.tab_unselect_text_color;
            itemText.setTextColor(getResources().getColor(textColorId));
            // 默认第一个选中
            if (i == 0) tab.select();
            tabLayout.addTab(tab);
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.d(LOG_TAG, "select: " + tab.getPosition());
                // 首页不需要滑动效果
                viewPager.setCurrentItem(tab.getPosition(), false);
                change(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                Log.d(LOG_TAG, "unselect: " + tab.getPosition());
                change(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }

            private void change(TabLayout.Tab tab, boolean isSelect) {
                int position = tab.getPosition();
                View customView = tab.getCustomView();
                ImageView itemImage = customView.findViewById(R.id.itemImage);
                int imageID = isSelect ? tabSelectedImages[position] : tabUnselectedImages[position];
                itemImage.setImageResource(imageID);
                TextView textView = customView.findViewById(R.id.itemText);
                int textColorId = isSelect ? R.color.black : R.color.tab_unselect_text_color;
                textView.setTextColor(getResources().getColor(textColorId));
            }
        });
    }
}